﻿using System;
using System.IO;
using System.Xml;

//自定义命名空间
using DreamCube.Foundation.Basic.Utility;

namespace BGN.PlugIn.Common
{
    public abstract class BasicConfiger
    {
        #region "静态方法"

        private static XmlDocument doc;

        /// <summary>
        /// 忽略线程安全问题
        /// </summary>
        public static XmlDocument Doc
        {
            get
            {
                if (doc == null)
                {
                    //即使找不到配置文件 也不需要报错
                    try
                    {
                        doc = new XmlDocument();
                        doc.Load(Path.Combine(MyDll.GetCurrentDllPath(), "SisenMESS_SinfoPlugIn.xml"));
                    }
                    catch (Exception ex)
                    {
                        MyLog.MakeLog(ex);
                    }
                }
                return doc;
            }
        }

        /// <summary>
        /// 根据Xpath和属性名称获取某个xml节点的属性值
        /// </summary>
        /// <param name="xpath"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static String GetPropertyValue(String xpath, String propertyName)
        {
            try
            {
                if (Doc == null) return String.Empty;
                XmlNode node = Doc.SelectSingleNode(xpath);
                if (node != null)
                {
                    XmlAttribute attr = node.Attributes[propertyName];
                    if (attr != null) return attr.Value;
                }
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
            return String.Empty;
        }

        /// <summary>
        /// 根据XPath获取某个节点的InnerText值
        /// </summary>
        /// <param name="xpath"></param>
        /// <returns></returns>
        public static String GetInnerText(String xpath)
        {
            try
            {
                if (Doc == null) return String.Empty;
                XmlNode node = Doc.SelectSingleNode(xpath);
                if (node != null) return node.InnerText;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
            return String.Empty;
        }

        #endregion

        /// <summary>
        /// 派生类必须重新此方法
        /// </summary>
        protected abstract String ParentXPath
        {
            get;
        }

        /// <summary>
        /// 获取公司名称
        /// </summary>
        /// <returns></returns>
        public String GetCompanyName()
        {
            return GetXmlConfigNodeInnerText("/Configs/Common/Company");
        }

        /// <summary>
        /// 获取系统ID号
        /// </summary>
        /// <returns></returns>
        public String GetSystemID()
        {
            return GetXmlConfigNodeInnerText("/Configs/Common/SystemID");
        }

        /// <summary>
        /// 获取插件配置的ID号 唯一ID，guid
        /// </summary>
        /// <returns></returns>
        public String GetID()
        {
            return GetPropertyValue(ParentXPath, "ID");
        }

        /// <summary>
        /// 获取插件配置的产品信息
        /// </summary>
        /// <returns></returns>
        public String GetProduct()
        {
            return GetXmlConfigNodeInnerText("Product");
        }

        /// <summary>
        /// 获取节点的文本内容
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public String GetXmlConfigNodeInnerText(String nodeName)
        {
            return GetInnerText(String.IsNullOrEmpty(nodeName) ? ParentXPath : ParentXPath + "/" + nodeName);
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="nodeName"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public String GetXmlConfigNodePropertyValue(String nodeName, String propertyName)
        {
            return GetPropertyValue(String.IsNullOrEmpty(nodeName) ? ParentXPath : ParentXPath + "/" + nodeName, propertyName);
        }
    }
}
